home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE25 / EX10.C < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  77 lines

  1. #include <genstub.c>
  2.  
  3. // Add these options to the menu.
  4. #define IDM_IDLE     401
  5. #define IDM_NORMAL   402
  6. #define IDM_HIGH     403
  7. #define IDM_REALTIME 404
  8.  
  9. //  Function that wastes CPU time.
  10. DWORD GoWasteSomeCPU(void)
  11. {
  12.     long i, j;
  13.     DWORD dwTicksNow = GetTickCount( );
  14.     for ( i = 0; i < 100000; i ++ )
  15.         for ( j = 0; j < 1000; j ++ );
  16.     return GetTickCount( ) - dwTicksNow;
  17. }
  18.  
  19. //  Windows message procedure.
  20. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  21. {
  22.    switch ( uMsg )
  23.    {
  24.          case WM_COMMAND:       // process menu items
  25.              {
  26.                  DWORD dwPriorityClass = 0;
  27.                  switch ( LOWORD( wParam ) )
  28.                  {
  29.                      case IDM_IDLE:
  30.                          dwPriorityClass = IDLE_PRIORITY_CLASS;
  31.                      break;
  32.                      case IDM_NORMAL:
  33.                          dwPriorityClass = NORMAL_PRIORITY_CLASS;
  34.                      break;
  35.                      case IDM_HIGH:
  36.                          dwPriorityClass = HIGH_PRIORITY_CLASS;
  37.                      break;
  38.                      case IDM_REALTIME:
  39.                          dwPriorityClass = REALTIME_PRIORITY_CLASS;
  40.                      break;
  41.                      case IDM_EXIT:
  42.                          DestroyWindow(hWnd);
  43.                      break;
  44.                   }
  45.                   if (dwPriorityClass != 0) {
  46.                      // Set the priority class.
  47.                      SetPriorityClass(GetCurrentProcess(), dwPriorityClass);
  48.                      SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  49.                   }
  50.              }
  51.              break;
  52.          case WM_USER:
  53.              {
  54.                  TCHAR szBuffer[128];
  55.                  static int iRow = 0;
  56.                  HDC hDC = GetDC( hWnd );
  57.                  DWORD dwPrCls = GetPriorityClass( GetCurrentProcess( ) );
  58.                  wsprintf( szBuffer, "Priority Class is %s, Time spent: %ld",
  59.                          ( dwPrCls==NORMAL_PRIORITY_CLASS ) ? "NORMAL" :
  60.                          ( ( dwPrCls==REALTIME_PRIORITY_CLASS ) ? "REALTIME" :
  61.                          ( ( dwPrCls==HIGH_PRIORITY_CLASS ) ? "HIGH" : "IDLE" ) ),
  62.                          lParam );
  63.                  TextOut( hDC, 0, iRow, szBuffer, lstrlen( szBuffer ) );
  64.                  ReleaseDC( hWnd, hDC );
  65.                  iRow += 20;
  66.                  if ( iRow > 220 )
  67.                     iRow = 0;
  68.              }
  69.              break;
  70.          case WM_DESTROY:
  71.                  PostQuitMessage( 0 );
  72.                  break;
  73.          default:
  74.              return (DefWindowProc(hWnd, uMsg, wParam, lParam));
  75.    }
  76.    return (NULL);
  77. }